package cn.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.shop.admin.common.util.CurrentUserUtil;
import cn.shop.admin.entity.backUserModule.BackUser;
import cn.shop.admin.req.ClearDataReq;
import cn.shop.admin.service.BackSettingService;
import cn.shop.admin.service.BackUserService;
import cn.shop.admin.service.IpWhitelistService;
import cn.shop.transcation.backSetting.entity.BackSetting;
import cn.shop.transcation.ipWhitelistModule.entity.IpWhitelist;
import cn.shop.utils.ApiResult;
import cn.shop.utils.constant.SystemSettingConstant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * ip 白名单(IpWhitelist)表控制层
 *
 * @author jzw
 * @since 2022-02-23 11:04:03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/ipWhitelist")
public class IpWhitelistController {

	/**
	 * 服务对象
	 */
	@Resource
	private IpWhitelistService ipWhitelistService;

	@Resource
	private BackUserService backUserService;
	@Resource
	private BackSettingService backSettingService;

	/**
	 * 查询所有数据
	 *
	 * @return 所有数据
	 * @author jzw
	 * @since 2022-02-23 11:04:03
	 */
	@GetMapping("queryAll")
	public ApiResult<List<Object>> queryAll() {
		LambdaQueryWrapper<IpWhitelist> query = Wrappers.lambdaQuery();
		query.eq(IpWhitelist::getUserId, CurrentUserUtil.getUserId()).
				select(IpWhitelist::getIp);
		return ApiResult.success(ipWhitelistService.listObjs(query));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author jzw
	 * @since 2022-02-23 11:04:03
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<IpWhitelist> selectOne(@PathVariable Serializable id) {
		return ApiResult.success(ipWhitelistService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param ipList 实体对象
	 * @return 新增结果
	 * @author jzw
	 * @since 2022-02-23 11:04:03
	 */
	@PostMapping("insert")
	public ApiResult<Map<String, Object>> insert(@RequestBody List<String> ipList) {
		return ApiResult.result(ipWhitelistService.insert(ipList));
	}
	/**
	 * 清除数据（资金管理 用户提现 日志）
	 * @author 王鹤然
	 * @date 2023/1/30 0030 下午 16:32
	 * @param req
	 * @return cn.shop.utils.ApiResult<java.util.Map<java.lang.String,java.lang.Object>>
	*/
	@PostMapping("clearData")
	public ApiResult<Map<String, Object>> clearData(@Validated @RequestBody ClearDataReq req) {
		// 校验交易密码是否正确
		String password = req.getPassword();
		BackSetting backSetting = backSettingService.getOneByKey(SystemSettingConstant.ADMIN_TRANSACTION_PASSWORD);
		if (StrUtil.isBlank(backSetting.getValue())) {
			return ApiResult.fail("请设置交易密码");
		}
		if (!DigestUtil.bcryptCheck(password, backSetting.getValue())) {
			return ApiResult.fail("交易密码不正确");
		}
		return ApiResult.result(ipWhitelistService.clearData(req.getClearTime()));
	}
}

